





package org.ceteca.explica.client.partners;

import org.ceteca.explica.client.ClientContext;
import org.ceteca.explica.client.gui.common.CommonClient;
import org.ceteca.explica.core.InterfaceErrors;
import org.ceteca.explica.core.common.BaseVO;
import org.ceteca.explica.core.partners.ContactOT;
import org.ceteca.explica.core.partners.InterfacePartners;
import org.ceteca.explica.core.partners.PaymentMethodOT;
import org.ceteca.explica.core.util.Converter;
import org.ceteca.explica.core.util.OperationResult;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
/**
 * @author Juan C. Díaz (juancarlosdiaz@ceteca.net) <br>
 *         Copyright (c) 2009 Fundación Centro Tecnolóxico da Carne
 * 
 * <br>
 * <br>
 *         This program is free software: you can redistribute it and/or modify
 *         it under the terms of the GNU General Public License as published by
 *         the Free Software Foundation, either version 3 of the License, or (at
 *         your option) any later version. <br/>
 *         This program is distributed in the hope that it will be useful, but
 *         WITHOUT ANY WARRANTY 
 *         <br>without even the implied warranty of
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *         General Public License for more details. <br/>
 *         You should have received a copy of the GNU General Public License
 *         along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
public class PaymentMethodClient extends CommonClient {

	// WIDGETS
	private Text entryPaymentMethodCode, entryPaymentMethodDescription, entryPaymentMethodNumberPayments,
			entryPaymentMethodDaysBetweenPayments, entryPaymentMethodDaysFirstPayment;
	private Button buttonCash, buttonBatch;
	
	public PaymentMethodClient(){

		super(PaymentMethodOT.class, InterfacePartners.EXPLICA_EDIT_PAYMENTMETHOD_GLADE_PATH, InterfacePartners.EDIT_PAYMENTMETHOD_DIALOG_NAME );
	}

	





	@Override
	public BaseVO getEntityData() {

		PaymentMethodOT entity = new PaymentMethodOT();
		if (this.entEditView != null) {
			this.getWidgets();
			entity.setDescription(entryPaymentMethodDescription.getText());
			entity.setDaysBetweenPayments((Converter.getInstance().parseLong(entryPaymentMethodDaysBetweenPayments.getText())));
			entity.setDaysFirstPayment(Converter.getInstance().parseLong(entryPaymentMethodDaysFirstPayment.getText()));
			entity.setNumberPayments(Converter.getInstance().parseLong(entryPaymentMethodNumberPayments.getText()));
			entity.setBatch(buttonBatch.getSelection());
			entity.setCash(buttonCash.getSelection());
			if (selectedEntity != null) {
				entity
						.setVersion(((PaymentMethodOT) this.selectedEntity)
								.getVersion());
				entity.setIdPaymentMethod(((PaymentMethodOT) this.selectedEntity)
						.getIdPaymentMethod());
			}
		}

		return entity;
	}

	@Override
	public OperationResult validateEntity() {
		OperationResult result=new OperationResult();
		boolean datosValidos=true;
		PaymentMethodOT entity= (PaymentMethodOT)getEntityData();

		if (entity.getNumberPayments()<0 || entity.getDaysBetweenPayments()<1 || entity.getDaysFirstPayment()<0)
			datosValidos=false;
		
		if ( datosValidos)
			result.addResultMessage(OperationResult.RESULT_OK, 
        			ClientContext.getInstance().getMsgCatalog().newResultMessage(InterfaceErrors.MSG_VALIDATE_OK));
		else
			result.addResultMessage(OperationResult.RESULT_ERROR, 
        			ClientContext.getInstance().getMsgCatalog().newResultMessage(InterfaceErrors.MSG_VALIDATE_ERROR));
		return result;
	}


	
	public void getWidgets() {
		
		boolean allOk=true;
		entryPaymentMethodCode = (Text) this.getWidget(
				InterfacePartners.EDIT_PAYMENTMETHOD_CODE_WIDGETNAME);
		entryPaymentMethodDescription = (Text) this.getWidget(
				InterfacePartners.EDIT_PAYMENTMETHOD_DESCRIPTION_WIDGETNAME);
		entryPaymentMethodDaysBetweenPayments = (Text) this.getWidget(
				InterfacePartners.EDIT_PAYMENTMETHOD_DAYSBETWEENPAYMENTS_WIDGETNAME);
		entryPaymentMethodNumberPayments = (Text) this.getWidget(
				InterfacePartners.EDIT_PAYMENTMETHOD_NUMBERPAYMENTS_WIDGETNAME);
		entryPaymentMethodDaysFirstPayment = (Text) this.getWidget(
				InterfacePartners.EDIT_PAYMENTMETHOD_DAYSFIRSTPAYMENT_WIDGETNAME);
		buttonCash=(Button) this.getWidget(InterfacePartners.EDIT_PAYMENTMETHOD_CASH_WIDGETNAME);
		buttonBatch=(Button) this.getWidget(InterfacePartners.EDIT_PAYMENTMETHOD_BATCH_WIDGETNAME);

		if (entryPaymentMethodCode == null){
			this.logger.error("Error getting entry Payment Method Code Widget");
			allOk=false;
		}
		if (entryPaymentMethodDaysBetweenPayments == null){
			this.logger.error("Error getting entry Payment Method Days Between Payments Widget");
			allOk=false;
		}
		if (entryPaymentMethodDaysFirstPayment == null){
			this.logger.error("Error getting entry Payment Method Days First Payment Widget");
			allOk=false;
		}
		if (entryPaymentMethodDescription == null){
			this.logger.error("Error getting entry Description Payment Widget");
			allOk=false;
		}
		if (entryPaymentMethodNumberPayments == null){
			this.logger.error("Error getting entry Number of Payments Widget");
			allOk=false;
		}
		if (buttonCash == null){
			this.logger.error("Error getting entry Cash check");
			allOk=false;
		}
		if (buttonBatch == null){
			this.logger.error("Error getting entry Batch check");
			allOk=false;
		}
		if (!allOk) {
			System.exit(-1);
		}
	}

	public void setWidgetsValues(BaseVO entity) {

		
		
		PaymentMethodOT ent = (PaymentMethodOT)entity;
		getWidgets();
		entryPaymentMethodCode.setText(Converter.getInstance().toString(ent.getIdPaymentMethod()));
		entryPaymentMethodDescription.setText(ent.getDescription());
		entryPaymentMethodDaysBetweenPayments.setText(Converter.getInstance().toString(ent.getDaysBetweenPayments()));
		entryPaymentMethodNumberPayments.setText(Converter.getInstance().toString(ent.getNumberPayments()));
		entryPaymentMethodDaysFirstPayment.setText(Converter.getInstance().toString(
				ent.getDaysFirstPayment()));
		buttonBatch.setSelection(ent.isBatch());
		buttonCash.setSelection(ent.isCash());
	}

	@Override
	public void setEditionMode(boolean enabled) {
		getWidgets();
		entryPaymentMethodCode.setEnabled(false);
		entryPaymentMethodDaysBetweenPayments.setEnabled(enabled);
		entryPaymentMethodDaysFirstPayment.setEnabled(enabled);
		entryPaymentMethodDescription.setEnabled(enabled);
		entryPaymentMethodNumberPayments.setEnabled(enabled);
		buttonBatch.setEnabled(enabled);
		buttonCash.setEnabled(enabled);
		
	}



	@Override
	public void buildSelectionSets() {
		
	}

	@Override
	public void init_specific_components() {
		// TODO Auto-generated method stub
		
	}







	@Override
	public void build_extension_fields_view() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void fillEntityData() {
		if (this.selectedEntity != null && this.entEditView != null) {
			getWidgets();
			setWidgetsValues((PaymentMethodOT) selectedEntity);
		}
	}
	
}
